<!DOCTYPE html>
<html>
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
	<meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
	<style type="text/css">
		body, html,#allmap {width: 100%;height: 100%;overflow: hidden;font-family:"微软雅黑";}
		#allmap{margin-top: 2px;}
	</style>
	<script type="text/javascript" src="http://api.map.baidu.com/api?v=2.0&ak=QdkTzxOfyhKC7awbR85DScR6"></script>
	<script type="text/javascript" src="http://api.map.baidu.com/library/SearchInfoWindow/1.5/src/SearchInfoWindow_min.js"></script>
	<link rel="stylesheet" href="http://api.map.baidu.com/library/SearchInfoWindow/1.5/src/SearchInfoWindow_min.css" />
	<!-- 加载城市列表 -->
	<script type="text/javascript" src="http://api.map.baidu.com/library/CityList/1.2/src/CityList_min.js"></script>
	<title>根据城市名设置地图中心点</title>
</head>
<body>
	<div id="r-result">
		地址:<input type="text" id="suggestId" value="请输入地址" style="width:30%;margin-right: 20px;" placeholder="天府软件园" />
		经度:<input type="text" id="lng"  readonly="readonly" style="width:20%;" placeholder="经度" />
		纬度:<input type="text" id="lat"  readonly="readonly" style="width:20%;" placeholder="纬度" />
	</div>
	<hr style="background: red;"/>
	<div id="searchResultPanel" style="border:1px solid #C0C0C0;width:150px;height:auto; display:none;"></div>
	<div id="allmap"></div>
	<!--城市列表-->
	<div class="sel_container"><strong id="curCity">北京市</strong> [<a id="curCityText" href="javascript:void(0)">更换城市</a>]</div>
	<div class="map_popup" id="cityList" style="display:none;">
		<div class="popup_main">
			<div class="title">城市列表</div>
			<div class="cityList" id="citylist_container"></div>
			<button id="popup_close"></button>
		</div>
	</div>
</body>
</html>
<script type="text/javascript">
	// 百度地图API功能
	function G(id) {
		return document.getElementById(id);
	}
    // 创建地址解析器实例
	var myGeo = new BMap.Geocoder();
	// 百度地图API功能
	var map = new BMap.Map("allmap");  // 创建Map实例
	map.centerAndZoom("四川",15);      // 初始化地图,用城市名设置地图中心点
	map.enableScrollWheelZoom();// 允许滚动
	map.enableContinuousZoom();// 允许拖拽
	map.addControl(new BMap.NavigationControl());  //添加默认缩放平
	map.addControl(new BMap.NavigationControl({anchor: BMAP_ANCHOR_TOP_RIGHT, type: BMAP_NAVIGATION_CONTROL_SMALL}));    
	
	//----------------------------------单击获取点击的经纬度
	map.addEventListener("click",function(e){
		map.clearOverlays();
		document.getElementById("lng").value=e.point.lng;
		document.getElementById("lat").value=e.point.lat;
		getAddrByPoint(e.point);
		var marker = new BMap.Marker(e.point);  // 创建标注
    	map.addOverlay(marker);               // 将标注添加到地图中
	    marker.setAnimation(BMAP_ANIMATION_BOUNCE); //跳动的动画
	});
	//----------------------------------
	
	//----------------------------------智能提示收索
	var ac = new BMap.Autocomplete(    //建立一个自动完成的对象
		{"input" : "suggestId"
		,"location" : map
	});

	ac.addEventListener("onhighlight", function(e) {  //鼠标放在下拉列表上的事件
	var str = "";
		var _value = e.fromitem.value;
		var value = "";
		if (e.fromitem.index > -1) {
			value = _value.province +  _value.city +  _value.district +  _value.street +  _value.business;
		}    
		str = "FromItem<br />index = " + e.fromitem.index + "<br />value = " + value;
		
		value = "";
		if (e.toitem.index > -1) {
			_value = e.toitem.value;
			value = _value.province +  _value.city +  _value.district +  _value.street +  _value.business;
		}    
		str += "<br />ToItem<br />index = " + e.toitem.index + "<br />value = " + value;
		G("searchResultPanel").innerHTML = str;
	});

	var myValue;
	ac.addEventListener("onconfirm", function(e) {    //鼠标点击下拉列表后的事件
	    var _value = e.item.value;
		myValue = _value.province +  _value.city +  _value.district +  _value.street +  _value.business;
		getPointByAddr(myValue,_value.province);
		G("searchResultPanel").innerHTML ="onconfirm<br />index = " + e.item.index + "<br />myValue = " + myValue;
		setPlace();
	});

	function setPlace(){
		map.clearOverlays();    //清除地图上所有覆盖物
		function myFun(){
			var pp = local.getResults().getPoi(0).point;    //获取第一个智能搜索的结果
			map.centerAndZoom(pp, 18);
			map.addOverlay(new BMap.Marker(pp));    //添加标注
		}
		var local = new BMap.LocalSearch(map, { //智能搜索
		  onSearchComplete: myFun
		});
		local.search(myValue);
	}
	//----------------------------------
	/*根据地址获取经纬度*/
	function getPointByAddr(city,provice){
	// 将地址解析结果显示在地图上,并调整地图视野
		myGeo.getPoint(city, function(point){
			document.getElementById("lng").value=point.lng;
			document.getElementById("lat").value=point.lat;
		}, provice);
	}
	/*根据经纬度获取地址*/
	function getAddrByPoint(pt){
		myGeo.getLocation(pt, function(rs){
			var addComp = rs.addressComponents;
			document.getElementById("suggestId").value=addComp.province + ", " + addComp.city + ", " + addComp.district + ", " + addComp.street + ", " + addComp.streetNumber;
		});   
	}
	
	
</script>